package com.gamota.mobi.fragment;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.Spinner;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.gamota.mobi.R;
import com.gamota.mobi.ReviewActivity;
import com.gamota.mobi.adapter.ErrorHandler;
import com.gamota.mobi.background.BackgroundTasks;
import com.gamota.mobi.event.Event.GetCommentSuccessEvent;
import com.gamota.mobi.event.Event.GetCommentsFailEvent;
import com.gamota.mobi.event.Event.RequestFailEvent;
import com.gamota.mobi.model.CommentItem;
import com.gamota.mobi.util.CommonUtils;
import com.gamota.mobi.util.Constants;
import com.gamota.mobi.util.Utils;
import com.gamota.mobi.validator.Form;
import com.gamota.mobi.validator.Validate;
import com.gamota.mobi.validator.validator.NotEmptyValidator;
import com.gamota.mobi.view.AlertDialogManager;
import com.gamota.mobi.view.LoadMoreListView;
import com.gamota.mobi.view.LoadMoreListView.OnLoadMoreListener;

import de.greenrobot.event.EventBus;

public class SupportFragment extends BaseFragment implements OnLoadMoreListener, OnClickListener{
	
	private AQuery aq;
	private BackgroundTasks bgTask;
	private String accessToken;
	private String appId;
	private List<CommentItem> commentList;
	private LoadMoreListView listView;
	private int START = 0;
	private EventBus eventBus;
	private Button btnWriteComment;
	private EditText editFeedback;
	private Button btnConfirm;
	private Form form;
	private Spinner spnErrorType;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		mParent = inflater.inflate(R.layout.feedback_fragment, null);
//		aq = new AQuery(mParent);
//		listView = (LoadMoreListView) mParent.findViewById(R.id.list_comment);
//		View v = inflater.inflate(R.layout.btn_write_comment, null);
//		btnWriteComment = (Button) v.findViewById(R.id.write_comment);
//		listView.addHeaderView(v);
//		aq.id(listView).adapter(adapter);
//		listView.setOnLoadMoreListener(this);
		bgTask = new BackgroundTasks(mContext);
		accessToken = Utils.getStaticAccessToken(getActivity());
		spnErrorType = (Spinner) mParent.findViewById(R.id.spn_feedback);
		spnErrorType.setAdapter(new ArrayAdapter<String>(mContext,android.R.layout.simple_list_item_1,mContext.getResources().getStringArray(R.array.feed_back_detail)));
		editFeedback = (EditText) mParent.findViewById(R.id.edit_feedback);
		btnConfirm = (Button) mParent.findViewById(R.id.btn_confirm);
		form = new Form();
		Validate serialValid = new Validate(editFeedback);
		serialValid.addValidator(new NotEmptyValidator(mContext));
		form.addValidates(serialValid);
		btnConfirm.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if(form.validate()){
					String message="";
					String selected = (String) spnErrorType.getSelectedItem();
						message +=mContext.getResources().getString(R.string.top_email_content)+" "+editFeedback.getText().toString()+"\n\n\n\n\n\n\n---------------\n"+mContext.getResources().getString(R.string.error_type)+ " "+selected+"\n";
					
					SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
					Date now = new Date();
					String date = simpleDateFormat.format(now);
					message += mContext.getResources().getString(R.string.content)+" "+editFeedback.getText().toString()+"\n";
					message += mContext.getResources().getString(R.string.usernamefeedback)+" "+"GUEST"+"\n";
					message += mContext.getResources().getString(R.string.devicename)+" "+CommonUtils.getDeviceName()+"\n";
					message += mContext.getResources().getString(R.string.android_version)+" "+CommonUtils.getDeviceVersion()+"\n";
					message += mContext.getResources().getString(R.string.version_app)+" "+Utils.getVersion(mContext)+"\n";
					message += mContext.getResources().getString(R.string.timetosend)+" "+date+"\n";
					Intent email = new Intent(Intent.ACTION_SEND);
					email.putExtra(Intent.EXTRA_EMAIL, new String[]{"hotro@gamota.vn"});		  
					email.putExtra(Intent.EXTRA_SUBJECT, "[Feedback] From: "+"GUEST"+" [GAMOTA Android v"+Utils.getVersion(mContext)+"]");
					email.putExtra(Intent.EXTRA_TEXT, message);
					email.setType("message/rfc822");
					mContext.startActivity(Intent.createChooser(email, "Choose an Email client :"));
//					editFeedback.setText("");
				}
			}
		});
		return mParent;
	}

	@Override
	protected void initVariables() {
		// TODO Auto-generated method stub
		eventBus = EventBus.getDefault();
		
//		appId = getArguments().getString(Constants.SLUG);
//		commentList = new ArrayList<CommentItem>();
//		adapter = new CommentAdapter(mContext, R.layout.comment_item, commentList);
	}

	@Override
	protected void initActions() {
		// TODO Auto-generated method stub
//		bgTask.getComments(accessToken, appId, START);
	}
	
	
	
	private static class ViewHolder {
		RatingBar rating;
		TextView userName;
		TextView time;
		TextView content;
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		START = START + 20;
		bgTask.getComments(accessToken, appId, START);
	}
	
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		eventBus.unregister(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.write_comment:
			if(Utils.getStaticIsLogin(getActivity())){
				Intent i = new Intent(getActivity(), ReviewActivity.class);
				i.putExtra(Constants.SLUG, appId);
				startActivity(i);
			} else {
				AlertDialogManager am = new AlertDialogManager(getActivity());
				am.showButtonsDialog(R.string.must_login, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						switch (which) {
						case DialogInterface.BUTTON_POSITIVE:
//							Intent intent = new Intent(getActivity(), LoginActivity.class);
//							startActivity(intent);
							break;
						case DialogInterface.BUTTON_NEGATIVE:
							dialog.dismiss();
							break;
						}
					}
				});
			}
			break;
		default:
			break;
		}
	}
}
